package com.exchange.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * @Author : RulerWood
 * @Data : 2024/4/27 - 21:16
 */
@Component
public class MapRabbitMQConfig {
    /**
     * 创建一个用来接收生产者消息的队列
     *
     * @return
     */
    @Bean("map-dead-queue")
    public Queue queue() {
        return new Queue("map-dead-queue", true, false, false, null);
    }

    /**
     * 创建死信交换机
     *
     * @return
     */
    @Bean("map-dead-exchange")
    public DirectExchange exchange() {
        return new DirectExchange("map-dead-exchange");
//        return ExchangeBuilder.directExchange("map-dead-exchange").build();
    }

    @Bean
    public Binding binding(@Qualifier("map-dead-queue") Queue mapQueue, @Qualifier("map-dead-exchange") Exchange mapExchange) {
        return BindingBuilder.bind(mapQueue).to(mapExchange).with("map-dead-routing-key").noargs();
    }

    /**
     * 为当前预约队列设置队列的信息
     *
     * @return
     */
    @Bean("ttl-queue")
    public Queue queuettl() {
        //设置了10分钟的队列
        //为当前的队列设置死信交换机
        HashMap<String, Object> map = new HashMap<>(3);
        map.put("x-message-ttl", 60000);
        map.put("x-dead-letter-exchange", "map-dead-exchange");
        map.put("x-dead-letter-routing-key", "map-dead-routing-key");
        return QueueBuilder.durable("ttl-queue").withArguments(map).build();
    }

    /**
     * 预约队列的交换机
     *
     * @return
     */
    @Bean("ttl-exchange")
    public Exchange exchangettl() {
        return ExchangeBuilder.directExchange("ttl-exchange").build();
    }

    /**
     * 预约队列的交换机和队列绑定
     *
     * @param queuettl
     * @param exchangettl
     * @return
     */
    @Bean
    public Binding binding1(@Qualifier("ttl-queue") Queue queuettl, @Qualifier("ttl-exchange") Exchange exchangettl) {
        return BindingBuilder.bind(queuettl).to(exchangettl).with("ttl-queue").noargs();
    }


}
